<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考试公告管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加考试公告</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="width: 500px;margin: 0 auto">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题">
        <el-input v-model="ruleForm.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="内容">
        <el-input v-model="ruleForm.content"
                  type="textarea"
                  :autosize="{ minRows: 8, maxRows: 10}"
                  placeholder="请输入内容"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="ruleForm.type" placeholder="请选择活动区域">
          <el-option label="报名时间公告" value="1"></el-option>
          <el-option label="打印准考证公告" value="2"></el-option>
          <el-option label="成绩查询公告" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button plain type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    </div>
  </div>
</template>
<style>
</style>
<script>
export default {
  data() {
    return {
      ruleForm: {
        title:'',
        content:'',
        type:'',
      },
      rules: {
        /*trigger: 'blur'触发方式,丢失焦点*/
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          /*向后端提交异步请求*/
          let  url="http://120.77.154.166:8080/info/add-new"
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response)=>{
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$notify({
                title: '成功',
                message: jsonResult.message,
                type: 'success'
              });
              this.resetForm(formName)
            }else {
              this.$notify.error({
                title: '警告',
                message: jsonResult.message,
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
